/*
 * Copyright (c) 2016. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.common.util.MD5Util;
import com.jiantong.user.dto.InsertUserGov;
import com.jiantong.user.impl.dao.UserDao;
import com.jiantong.user.impl.manager.UserManager;
import com.jiantong.user.impl.manager.UserManagerWeb;
import com.jiantong.user.model.*;
import com.jiantong.user.service.UserWriteService;

import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Author:  <a href="mailto:i@terminus.io">jlchen</a>
 * Date: 2016-01-30
 */
@Service
@Slf4j
@RpcProvider
public class UserWriteServiceImpl implements UserWriteService<User> {

    private final UserDao userDao;
    private final UserManager userManager;
    private final UserManagerWeb userManagerWeb;

    @Autowired
    public UserWriteServiceImpl(UserDao userDao, UserManager userManager, UserManagerWeb userManagerWeb) {
        this.userDao = userDao;
        this.userManager = userManager;
        this.userManagerWeb = userManagerWeb;
    }


    /**
     * 创建新用户
     *
     * @param user 用户
     * @return 新用户id
     */
    @Override
    public Response<Long> create(User user) {
        try {
            if (StringUtils.hasText(user.getPassword())) {  //对密码加盐加密
                user.setPassword(MD5Util.encrypt(user.getPassword()));
            }
            userDao.create(user);
            return Response.ok(user.getId());
        } catch (DuplicateKeyException e) {
            log.error("failed to create {}, cause:{}", user, Throwables.getStackTraceAsString(e));
            return Response.fail("user.loginId.duplicate");
        } catch (Exception e) {
            log.error("failed to create {}, cause:{}", user, Throwables.getStackTraceAsString(e));
            return Response.fail("user.create.fail");
        }
    }

    /**
     * 更新用户
     *
     * @param user 用户
     * @return 是否更新成功
     */
    @Override
    public Response<Boolean> update(User user) {
        try {
            userDao.update(user);
            return Response.ok(Boolean.TRUE);
        } catch (Exception e) {
            log.error("failed to update {}, cause:{}", user, Throwables.getStackTraceAsString(e));
            return Response.fail("user.update.fail");
        }
    }

    /**
     * 更新个人资料信息 养殖端
     *
     * @param avatarMap           头像
     * @param realName            姓名
     * @param gender              性别
     * @param identityCardNumber  身份证
     * @param email               邮箱
     * @param corporateCharterMap 公司执照
     * @return 是否更新成功,
     */
    @Override
    public Response<Boolean> updatePersonalData(Long id, Map<String, Map<String, String>> avatarMap, String realName, Integer gender, String identityCardNumber, String email, Map<String, Map<String, String>> corporateCharterMap) {
        try {
            Boolean b = userManager.updatePersonalData(id, avatarMap, realName, gender, identityCardNumber, email, corporateCharterMap);
            return Response.ok(b);
        } catch (Exception e) {
            log.error("failed.update avatar{},realName{},gender{},identityCardNumber{},email{},corporateCharter{},case:{}",
                    avatarMap, realName, gender, identityCardNumber, email, corporateCharterMap, Throwables.getStackTraceAsString(e));
            return Response.fail("personal data update fail");
        }
    }

    /**
     * 更新个人资料信息 政府端
     *
     * @param avatarMap          头像
     * @param realName           姓名
     * @param gender
     * @param identityCardNumber 身份证性别
     * @param email              邮箱
     * @return 是否更新成功,
     */
    @Override
    public Response<Boolean> updatePersonalData(Long id, Map<String, Map<String, String>> avatarMap, String realName, Integer gender, String identityCardNumber, String email) {
        try {
            Boolean b = userManager.updatePersonalData(id, avatarMap, realName, gender, identityCardNumber, email);
            return Response.ok(b);
        } catch (Exception e) {
            log.error("failed.update avatar{},realName{},gender{},identityCardNumber{},email{},case:{}",
                    avatarMap, realName, gender, identityCardNumber, email, Throwables.getStackTraceAsString(e));
            return Response.fail("personal data update fail");
        }
    }

    /**
     * web端添加政府用户
     * @param user
     * @param userProfile
     * @param organizationUser
     * @param departmentUser
     * @return
     */
    @Override
    public Response<Boolean> createGov(User user, UserProfile userProfile, OrganizationUser organizationUser, DepartmentUser departmentUser,AppUserRole appUserRole,UserRole userRole) {
        try {
           Boolean b = userManagerWeb.createOrganizationUser(user, userProfile, organizationUser, departmentUser,appUserRole,userRole);

            return Response.ok(b);
        } catch (Exception e) {
            log.error("failed.update avatar{},realName{},gender{},identityCardNumber{},email{},case:{}",
                    Throwables.getStackTraceAsString(e));
            return Response.ok(false);
        }
    }

    @Override
    public Response<Boolean> createOrganizationUserList(List<InsertUserGov> insertUserGovList) {
        try {
            Boolean b = userManagerWeb.createOrganizationUserList(insertUserGovList);

            return Response.ok(b);
        } catch (Exception e) {
            log.error("failed.update avatar{},realName{},gender{},identityCardNumber{},email{},case:{}",
                    Throwables.getStackTraceAsString(e));
            return Response.ok(false);
        }
    }

    /**
     * web端添加养殖用户
     * @param user
     * @param userProfile
     * @param organizationUser
     * @return
     */
    @Override
    public Response<Boolean> createFac(User user, UserProfile userProfile, OrganizationUser organizationUser) {
        try {
           Boolean b = userManagerWeb.createBreedFactoryUser(user, userProfile, organizationUser);

            return Response.ok(b);
        } catch (Exception e) {
            log.error("failed.update avatar{},realName{},gender{},identityCardNumber{},email{},case:{}",
                    Throwables.getStackTraceAsString(e));
            return Response.ok(false);
        }
    }

    /**
     * web端更新用户信息
     * @param user
     * @param userProfile
     * @param organizationUser
     * @param departmentUser
     * @return
     */
    @Override
    public Response<Boolean> updateuser(User user, UserProfile userProfile, OrganizationUser organizationUser, DepartmentUser departmentUser) {
        try {
            Boolean b = userManagerWeb.updateUser(user, userProfile, organizationUser, departmentUser);

            return Response.ok(b);
        } catch (Exception e) {
            log.error("failed.update avatar{},realName{},gender{},identityCardNumber{},email{},case:{}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("personal data update fail");
        }
    }


}
